package cn.iocoder.an.module.business.service.auditlevel;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.an.module.business.dal.dataobject.audittype.AuditConfigDO;
import cn.iocoder.an.module.business.dal.mysql.audittype.AuditConfigMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.an.module.business.controller.admin.auditlevel.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.auditlevel.AuditLevelDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.auditlevel.AuditLevelMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 审核等级 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class AuditLevelServiceImpl implements AuditLevelService {

    @Resource
    private AuditLevelMapper auditLevelMapper;


    @Resource
    private AuditConfigMapper auditConfigMapper;

    @Override
    public Long createAuditLevel(AuditLevelSaveReqVO createReqVO) {
        removeIds(createReqVO);
        // 插入
        AuditLevelDO auditLevel = BeanUtils.toBean(createReqVO, AuditLevelDO.class);
        auditLevelMapper.insert(auditLevel);

        // 返回
        return auditLevel.getId();
    }

    @Override
    public void updateAuditLevel(AuditLevelSaveReqVO updateReqVO) {
        //移除重复的id
        removeIds(updateReqVO);
        // 校验存在
        validateAuditLevelExists(updateReqVO.getId());
        // 更新
        AuditLevelDO updateObj = BeanUtils.toBean(updateReqVO, AuditLevelDO.class);
        auditLevelMapper.updateById(updateObj);
    }

    public void removeIds(AuditLevelSaveReqVO reqVO){
        List<Long> unique = Optional.ofNullable(reqVO.getConfigId())
                .orElse(Collections.emptyList())
                .stream()
                .distinct()
                .collect(Collectors.toList());
        reqVO.setConfigId(unique);
    }

    @Override
    public void deleteAuditLevel(Long id) {
        // 校验存在
        validateAuditLevelExists(id);
        // 删除
        auditLevelMapper.deleteById(id);
    }

    @Override
        public void deleteAuditLevelListByIds(List<Long> ids) {
        // 删除
        auditLevelMapper.deleteByIds(ids);
        }


    private void validateAuditLevelExists(Long id) {
        if (auditLevelMapper.selectById(id) == null) {
            throw exception(AUDIT_LEVEL_NOT_EXISTS);
        }
    }

    @Override
    public AuditLevelDO getAuditLevel(Long id) {
        AuditLevelDO auditLevelDO = auditLevelMapper.selectById(id);
        List<Long> configId = auditLevelDO.getConfigId() != null ? auditLevelDO.getConfigId() : new ArrayList<>();
        configId.add(0L);//防止空报错
        LambdaQueryWrapperX<AuditConfigDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.in(AuditConfigDO::getId,configId);
        List<AuditConfigDO> auditConfigDOS = auditConfigMapper.selectList(wrapperX);
        auditLevelDO.setConfigDOList(auditConfigDOS);
        return auditLevelDO;
    }

    @Override
    public PageResult<AuditLevelDO> getAuditLevelPage(AuditLevelPageReqVO pageReqVO) {
        return auditLevelMapper.selectPage(pageReqVO);
    }

}